package wxdb.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;

public class DelUser extends JDialog{
	
	private JButton delButton = new JButton("Delete");
	private JButton clButton = new JButton("Cancel");
	private JLabel delLabel = new JLabel("Delete user :");
	private JComboBox cmb = new JComboBox();   

	private JPanel pane = new JPanel();
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);

		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(delLabel, constraints);
		pane.add(delLabel);
		
		constraints=new GridBagConstraints();
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(cmb, constraints);
		pane.add(cmb);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		gridbag.setConstraints(delButton, constraints);
		pane.add(delButton);
		
		constraints=new GridBagConstraints();
		constraints.insets=new Insets(5,5,5,5);
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gridbag.setConstraints(clButton, constraints);
		pane.add(clButton);
		
		setLocation(400,200);
		setContentPane(pane);
	}
	
	private void initListener(){
		ActListener actListener = new ActListener();
		delButton.addActionListener(actListener);
		clButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == delButton)
				delButtonRun();
			if(e.getSource() == clButton){
				dispose();
			}
		}
	}
	
	private void delButtonRun(){
		Global.delUser((String)cmb.getSelectedItem());
		this.dispose();
	}
	
	public DelUser(){
		ArrayList<String> al = Global.getUsers();
		for (Iterator<String> iter = al.iterator(); iter.hasNext();) {
			String name = iter.next(); 
			if(name.equals("Administrator"))
				continue;
			cmb.addItem(name);
		}
		setTitle("Administrator-DeleteUser");
		setSize(200,200);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);		
	}
}
